<?php

namespace app\controller\admin\cnf;

use core\controller\AdminBase;
use app\bll\ComponentExtend as BllComponentExtend;
use app\bll\ComponentExtendGroup as BllComponentExtendGroup;
use Exception;
use Noodlehaus\Config;
use support\View;

class ComponentExtend extends AdminBase
{
    public function index()
    {
        $bllComponentExtend = new BllComponentExtend();
        $extends = $bllComponentExtend->paginator(
            title: request()->get('title', ''),
            ident: request()->get('ident', ''),
            groupid: request()->get('groupid', '') == '' ? null : request()->get('groupid', '')
        );

        $bllComponentExtendGroup = new BllComponentExtendGroup();
        $groups                  = $bllComponentExtendGroup->select();
        View::assign('extends', $extends);
        View::assign('groups', $groups);
        return $this->view();
    }



    public function install()
    {
        if (is_post()) {
            try {
                $bllComponentExtend = new BllComponentExtend();
                $bllComponentExtend->install();
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    /**
     * 在线升级
     */
    public function onlineUpgrade()
    {
        try {
            $id                 = request()->input('id');
            $bllComponentExtend = new BllComponentExtend();
            $bllComponentExtend->onlineUpgrade($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 手动升级
     *
     * @return void
     */
    public function handleUpgrade()
    {
        if (is_post()) {
            try {
                $bllComponentExtend = new BllComponentExtend();
                $bllComponentExtend->handleUpgrade(
                    request()->input('id')
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    /**
     * 配置扩展
     *
     * @return void
     */
    public function setExtend()
    {
        $id                 = request()->input('id');
        $bllComponentExtend = new BllComponentExtend();
        $extendinfo         = $bllComponentExtend->find($id);


        if (is_post()) {
            try {
                $config = Config::load(get_extend_path() . $extendinfo['ident'] . '/config.ini');
                $data = request()->post();
                foreach ($data as $key => $d) {
                    $config->set('value.' . $key, $d);
                }
                $config->toFile(get_extend_path() . $extendinfo['ident'] . '/config.ini');
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $config = Config::load(get_extend_path() . $extendinfo['ident'] . '/config.ini');

            View::assign('config', $config->all());

            return $this->view(
                frame: false
            );
        }
    }

    public function unInstall()
    {
        try {
            $id                 = request()->input('id');
            $bllComponentExtend = new BllComponentExtend();
            $bllComponentExtend->unInstall($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
